import { defineStore } from 'pinia';
import { InitDict } from '#/api/core/dict';

// 字典项目枚举
const DictEnum = {
  // 状态字典
  STATE: 'state',
  // 用户角色字典
  USER_ROLE: 'userRole',
  // 分类字典
  CLASS: 'class',
  // 发布状态
  PUBLISH_STATUS: 'publishStatus',
  // 借阅状态
  BORROW_STATE: 'borrowState',
};

type DictEnumKeys = keyof typeof DictEnum;
type DictItem = {
  id: string; // 广告类型ID
  label: string; // 广告标签
  value: string; // 广告值
};

export const useDictStore = defineStore('dictStore', {
  state: () => ({
    dict: {
      [DictEnum.STATE]: [] as DictItem[],
    },
  }),
  getters: {
    // 获取典项标签
    getDictLabel: (state) => (dictType: DictEnumKeys, value: string) => {
      console.log('getDictLabel', dictType, value);
      const type = DictEnum[dictType];
      const items = state.dict[type] || [];
      const item = items.find((item: DictItem) => item.value == value);
      console.log(item);
      return item ? item.label : '';
    },
    getDictClassList: (state) => (dictType: DictEnumKeys) => {
      console.log('getDictClassList', dictType);
      const type = DictEnum[dictType];
      const list = state.dict[type] || [];
      return list;
    },
  },
  actions: {
    // 获取字典项
    async initDictQuery() {
      let res: any = await InitDict();
      this.dict = res.data;
      console.log('this.dict', this.dict);
    },
  },
});
